﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.Net.Http.Formatting.Parsers;

namespace GoDive.Contract
{
    [DataContract]
    public class PagedListContract<T>
    {
        public PagedListContract()
        { }

        private int pageSize = 20;
        private int pageIndex = 1;
        private bool hasPrevPage;
        private bool hasNextPage;

        [DataMember(Name = "Total")]
        public int Total { get; set; }
        [DataMember(Name = "PageSize")]
        public int PageSize
        {
            get { return pageSize; }
            set { pageSize = value; }
        }
        [DataMember(Name = "PageIndex")]
        public int PageIndex
        {
            get { return pageIndex; }
            set { pageIndex = value; }
        }

        [DataMember(Name = "HasPrevPage")]
        public bool HasPrevPage
        {
            get
            {
                if (this.pageIndex <= this.PageTotalCount + 1 && this.pageIndex > 1)
                {
                    hasPrevPage = true;
                }
                return hasPrevPage;
            }
            set { hasPrevPage = value; }
        }
        [DataMember(Name = "HasNextPage")]
        public bool HasNextPage
        {
            get
            {
                int start = this.pageIndex * this.pageSize;
                if (this.Total > start)
                {
                    hasNextPage = true;
                }
                return hasNextPage;
            }
            set { hasNextPage = value; }
        }


        [DataMember(Name = "PageTotalCount")]
        public int PageTotalCount
        {
            get
            {
                return this.CalculatePageTotalCount();
            }
            set { ; }
        }

        [DataMember(Name = "Rows")]
        public IEnumerable<T> Rows { get; set; }

        private int CalculatePageTotalCount()
        {
            return this.Total % pageSize == 0 ? Total / pageSize : Total / pageSize + 1;
        }
    }
}
